package com.hks.service.impl;

import com.hks.common.utils.JwtUtil;
import com.hks.common.utils.RedisCache;
import com.hks.common.utils.ResponseResult;
import com.hks.pojo.LoginUser;
import com.hks.pojo.Menu;
import com.hks.pojo.User;
import com.hks.service.LoginService;
import com.hks.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private MenuService menuService;


    /**
     * 登录
     * @param user
     * @return
     */
    @Override
    public ResponseResult login(User user) {
        //校验
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());
        //使用ProviderManager auth方法进行校验
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //判断校验结果
        if (Objects.isNull(authenticate)){
            throw new RuntimeException("用户名或密码错误");
        }

        //获取当前登录对象
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        Integer loginUserId = loginUser.getUser().getId();
        //将当前对象的id获取并生成token传到前端
        String token = JwtUtil.createJWT(String.valueOf(loginUserId));
        //将token封装到map中并传到前端
        Map<String, String> map = new HashMap<>();
        map.put("token",token);

        //在redis中缓存用户信息
        redisCache.setCacheObject("login:"+loginUserId,loginUser);

        return new ResponseResult(200,"登录成功",map);
    }

    /**
     * 登出
     * @return
     */
    @Override
    public ResponseResult logout() {
        //根据security上下文获取当前登录对象
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //移除在redis缓存中的用户信息
        redisCache.deleteObject("login:"+loginUser.getUser().getId());
        return new ResponseResult(200,"退出成功");
    }

    /**
     * 返回用户信息
     */
    @Override
    public ResponseResult getInfo() {
        //根据security上下文获取当前登录对象
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //指定返回的用户信息
        Map<String, Object> map = new HashMap<>();
        User user = new User();
        user.setAvatar(loginUser.getUser().getAvatar());
        user.setUsername(loginUser.getUser().getUsername());
        user.setNickName(loginUser.getUser().getNickName());
        user.setMenus(menuService.getMenuListByUserId(loginUser.getUser().getId()));
        map.put("userInfo",user);
        return new ResponseResult(200,"成功获取",map);
    }
}
